package com.hdu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hdu.entity.Chat;
import com.hdu.mapper.ChatMapper;
import com.hdu.pojo.Friend;
import com.hdu.pojo.PageBean;
import com.hdu.service.IChatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 聊天记录实体类 服务实现类
 * </p>
 *
 * @author xiaobai
 * @since 2024-05-20
 */
@Service
@RequiredArgsConstructor
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements IChatService {

    private final ChatMapper chatMapper;

    @Override
    public List<Chat> page(Integer page, Integer pageSize, String id, String sender) {
        //1. 设置分页参数
        PageHelper.startPage(page, pageSize);

        Chat chat = new Chat();
        chat.setId(id);
        chat.setSender(sender);

        //2. 执行查询
        Page<Chat> p = (Page<Chat>) chatMapper.selectChat(chat);

        //3. 封装PageBean对象
        PageBean<Chat> pageBean = new PageBean<>(p.getTotal(), p.getResult());
        List<Chat> chatList = pageBean.getRows();
        Collections.reverse(chatList);

        return chatList;
    }


    @Override
    public List<Friend> friend(String tID) {
        List<Friend> classFriend = chatMapper.classList(tID);
        List<Friend> friendList = new ArrayList<>(classFriend);
        for (Friend friend : classFriend) {
            friendList.addAll(chatMapper.studentList(friend.getId()));
            friendList.addAll(chatMapper.teacherList(friend.getId(), tID));
        }
        return friendList;
    }
}
